<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled"  style="margin-bottom: 50px;">
      <a-form :form="form" slot="detail">
        <a-row v-for="(item,index) in cdList" :key="index">
          <a-col :span="24">
            <a-form-item label="产地信息名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="item.cdName" :maxLength="10" placeholder="请输入产地信息名称(10个字以内)"   @blur="checkInput('cdName',index)"   :style="index>0?'float: left;width: 75%':''" ></a-input>
              <a-button class="new-btn" v-if="index>0" type="dashed" @click="deletePro(index)" style="margin-left: 10px;vertical-align: super;color:#ff0000;">
                <a-icon type="minus"/>
                删除记录
              </a-button>
              <div class="ant-form-explain" style="color: #ff0000;" v-if="!item.cdName">{{messageList[index].cdName}}</div>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="产地信息图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <span style="color: #ff0000">(图片大小：＜1M  最多可上传3张)</span>
              <j-image-upload bizPath="commodity/cdInfo"  isMultiple :number="3" aria-valuemax="3" v-model="item.cdImg" ></j-image-upload>
              <div class="ant-form-explain" style="color: #ff0000;" v-if="!item.cdImg">{{messageList[index].cdImg}}</div>
            </a-form-item>
          </a-col>
         <!-- <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>-->
        </a-row>
        <a-row v-if="cdList.length<8">
          <a-col :span="24"  align="center" >
            <a-button class="new-btn" type="dashed" @click="addNewPro">
              <a-icon type="plus"/>
              添加新记录
            </a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'QyCommodityCdInfoForm',
    components: {
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/qy/qyCommodityCdInfo/add",
          edit: "/qy/qyCommodityCdInfo/edit",
          queryById: "/qy/qyCommodityCdInfo/queryById",
          list: "/qy/qyCommodityCdInfo/list"
        },
        cdList:[],
        messageList:[],
        typeMap:{
          "cdName":"产地名称",
          "cdImg":"产地图片"
        },
        commodityId:''
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      //this.showFlowData();
    },
    methods: {
      add () {
        //this.edit({});
      },
      edit (record) {
        /*this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'commodityId','cdName','cdImg','sort'))
        })*/
      },
      //渲染流程表单数据
      showFlowData(){
       /* if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }*/
      },
      submitForm () {

        const that = this;
        if(this.cdList){
          var isUp = true;
          for(var k in that.cdList){
            var pro = that.cdList[k];
            isUp= that.checkInput('cdName',k);
            if(!isUp){
              break;
            }
            isUp= that.checkInput('cdImg',k);
            pro.commodityId = that.commodityId;
          }
          if(isUp){//提交
            that.confirmLoading = true;
            that.$emit("changeLoading",true);
            httpAction(that.url.edit,that.cdList,'put').then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit("changeNext",this.commodityId);
              }else{
                that.$message.warning(res.message);
              }
              that.$emit("changeLoading",false);
            }).finally(() => {
              that.confirmLoading = false;
            });
          }
        }

      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'cdName','cdImg'))
      },
    getDetail(commodityId){
      let params = {commodityId:commodityId};
      getAction(this.url.list,params).then((res)=>{
        if(res.success){
          if(res.result.length>0){
            this.cdList= res.result;
            for(var jk in this.cdList){
              this.messageList.push({
                "cdName":"",
                "cdImg":""
              });
            }
          }else{
            this.cdList= [{
              "cdName":"",
              "cdImg":""
            }];
            this.messageList = [{
              "cdName":"",
              "cdImg":""
            }];
          }

        }
      });
      this.commodityId = commodityId;
    },addNewPro(){
        this.cdList.push({
          "cdName":"",
          "cdImg":""
        });
        this.messageList.push({
          "cdName":"",
          "cdImg":""
        });
      },deletePro(idx){
        this.cdList.splice(idx,1);
        this.messageList.splice(idx,1);
      },checkInput(cold,idx){
        if(this.cdList[idx][cold]){
          this.messageList[idx][cold] = "";
          return true;
        }else{
          this.messageList[idx][cold] = this.typeMap[cold]+"不能为空";
          return false
        }

      }
    }
  }
</script>